package org.androidpush.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.androidpush.R;
import org.androidpush.activity.ChatActivity;
import org.androidpush.adapter.MeAdapter;
import org.androidpush.greendao.Student;
import org.androidpush.greendao.StudentDao;
import org.androidpush.utils.ConstantUtils;
import org.androidpush.utils.DBUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MeFragment extends Fragment {

    private ListView infolist, otherlist;
    private View view;
    private List<Map<String, Object>> datalist;
    private StudentDao studentDao;
    private String userId;
    private TextView userIdTV, userNameTV;

    private int[] infoImgs = {R.drawable.ic_sex, R.drawable.ic_img, R.drawable.ic_email, R.drawable.ic_info, R.drawable.ic_other},
            otherImgs = {R.drawable.ic_chage, R.drawable.ic_setting, R.drawable.ic_back};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.xml_me, container, false);
        init();

        otherlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), NotificationActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.push_left_in,
                        R.anim.push_left_out);
            }
        });
        return view;
    }

    private void init() {
        infolist = (ListView) view.findViewById(R.id.id_me_infolist);
        otherlist = (ListView) view.findViewById(R.id.id_me_otherlist);
        userIdTV = (TextView) view.findViewById(R.id.id_me_userid);
        userNameTV = (TextView) view.findViewById(R.id.id_me_username);
        studentDao = DBUtils.getStudentDao(this.getActivity());
        SharedPreferences sharedPreferences = ConstantUtils.getSharedPreferences(getActivity());
        userId = sharedPreferences.getString(ConstantUtils.USER_ID, "");
        initData();
    }

    private void initData(){
        Student student =null;
        String[] otherValues = {"", "", ""};
        List<Student> students = studentDao.queryRaw("WHERE STU_ID = ?", userId);
        if (students.size() > 0) {
            student = students.get(0);
        }
        if (student != null) {
            Log.d("Student",student.toString());
            userIdTV.setText(userId);
            userNameTV.setText(student.getStuName());
            String values[] = {student.getStuSex(), "", student.getStuEmail(), student.getStuCreditCount().toString(), "3.2"};
            infolist.setAdapter(new MeAdapter(view.getContext(),
                    getMeData(infoImgs, getResources().getStringArray(R.array.strArray_infoNames), values)));
            otherlist.setAdapter(new MeAdapter(view.getContext(),
                    getMeData(otherImgs, getResources().getStringArray(R.array.strArray_otherNames), otherValues)));
        } else {
            String values[] = {"", "", "", "", ""};
            infolist.setAdapter(new MeAdapter(view.getContext(),
                    getMeData(infoImgs, getResources().getStringArray(R.array.strArray_infoNames), values)));
            otherlist.setAdapter(new MeAdapter(view.getContext(),
                    getMeData(otherImgs, getResources().getStringArray(R.array.strArray_otherNames), otherValues)));
        }
    }

    private List<Map<String, Object>> getMeData(int[] imgs, String[] names, String[] values) {
        datalist = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imgs.length; i++) {
            HashMap<String, Object> itemData = new HashMap<String, Object>();
            itemData.put("name", names[i]);
            itemData.put("value", values[i]);
            itemData.put("img", imgs[i]);
            datalist.add(itemData);
        }
        return datalist;
    }
}
